// pages/mine/shop/mycard/recommend.js
// pages/mine/shop/card/recommend.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    mycard_id: '',
    show: 1,
    hide: 0,
    tname: '选择奖励套餐',
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    that.setData({
      card_id: options.id,
      token: extConfig.appid,
      show: 1,
      hide: 0,
      tname: '选择奖励套餐',
    });
  },
  selecttype: function () {
    var that = this;
    if (that.data.show == 1) {
      that.setData({
        show: 0,
        hide: 1,
        tname: '填写奖励金额',
      })
    } else {
      that.setData({
        show: 1,
        hide: 0,
        tname: '选择奖励套餐',
      })
    }
  },

  // 获取 会员卡的相关信息
  getCardRcharge: function () {
    let that = this;
    app.ihttpGet('https://www.58zltc.com/api.php/MyCard/getRecommend/openid/' + app.globalData.openid + '/card_id/' + that.data.card_id, function (res) {
      that.setData({
        meal: res.data.meal,
        recommendpay: res.data.recommendpay,
        recommendmeal: res.data.recommendmeal,
        pay:res.data.pay
      })
      wx.hideLoading();
    });
  },

  bindChange: function (e) {
    let that = this;
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    console.log(e.detail.value);
    this.data.money = e.detail.value;
    this.data.recharge_id = 0;

  },
  radiochange: function (e) {
    var recharge_id = '';
    console.log(e.detail.value);
    this.data.recharge_id = e.detail.value;
    this.data.money = 0;

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    wx.showLoading({
      title: '加载中…',
    })

    this.getCardRcharge();
  },

  cardRecharge: function () {
    let that = this;
    var a = that.data.money;
    var b = that.data.recharge_id;
    console.log(a);
    if (!a && !b) {
      wx.showToast({
        title: '请输入奖励金额!',
        icon: 'none',
        duration: 1500
      })
    } else {
      if (a) {
        that.data.recharge_id = 0;
      } else if (b) {
        that.data.money = 0;
      }

      var post_data = {};
      post_data.openid = app.globalData.openid;
      post_data.money = that.data.money;//充值金额
      post_data.meal_id = that.data.recharge_id;//充值优惠记录id
      post_data.card_id = that.data.card_id;
      app.ihttpPost("https://www.58zltc.com/api.php/Card/AddRecommend", post_data, function (res) {
        if (res.data.err_code == 400) {
          app.iModel(res.data.err_msg);
        } else {
          app.iModel(res.data.err_msg);

          setTimeout(function () {
            wx.navigateBack({})
          }, 1000)

        }
      })
    }
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})
